package com.example.lombokdemo.aop;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Aspect
@Component
@Slf4j
public class ControllerLogAspect {
    @Pointcut("execution(* com.example.lombokdemo.controller.*.*(..))")
    public void pointcut() {
    }


    @Around("pointcut()")
    public Object before(ProceedingJoinPoint joinPoint) {
        Object[] requestInfo = joinPoint.getArgs();
        try {
            Object resultInfo = joinPoint.proceed();
            log.info("{}请求,IN:{},OUT:{}", joinPoint.getSignature().getName(), JSONUtil.toJsonStr(requestInfo),JSONUtil.toJsonStr(resultInfo));
            return resultInfo;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            log.info("{}请求,IN:{},报错信息:{}",joinPoint.getSignature().getName(),JSONUtil.toJsonStr(requestInfo),throwable.getMessage());
            return null;
        }
    }
}
